﻿/*
培训任务的添加修改
*/
//
angular.module('trainingTaskComplete', ['vModal', 'loadModule','PensonSelector','Common','knowledgebaseS'])
// 设置焦点 标签用
.directive('setFocus', function () {
     return function (scope, element) {
         element[0].focus();
     };
 })
 .directive("trainingTaskComplete", ["$rootScope", "$stateParams", "$timeout", "Msg","RequestTrainService", "sysUserInfo",function ($rootScope, $stateParams, $timeout, Msg,RTS,sysUserInfo) {
     return {
         restrice: "AE",
         scope: {
             isSaveBtn: "=",
             taskObj: "=",   // 培训计划对象
             onClose: "&"    //关闭窗口       
         },
         templateUrl: "/system/directive/train/trainingtask/training_task_complete.html",
         link: function (scope, element, attr) {
            scope.comObj=angular.copy(scope.taskObj);

            scope.mousePosition = function (ev) {
                if (!ev) {
                    ev = window.event;
                }

                if (ev.type != "click") {
                    return;
                }
                var width = 0;
                var height = 0;
                if (ev.pageX || ev.pageY) {//firefox、chrome等浏览器
                    if (document.documentElement.clientWidth - ev.pageX < 450) {
                        width = ev.pageX - (450 - (document.documentElement.clientWidth - ev.pageX)) - 20;
                    } else {
                        width = ev.pageX;
                    }
                    if (document.documentElement.clientHeight - ev.pageY < 480) {
                        height = ev.pageY - (480 - (document.documentElement.clientHeight - ev.pageY)) - 20;
                    } else {
                        height = ev.pageY;
                    }

                    return { "left": width + "px", "top": height + "px", "position": "fixed" };
                }

                return {// IE浏览器
                    "left": ev.clientX + document.body.scrollLeft - document.body.clientLeft,
                    "top": ev.clientY + document.body.scrollTop - document.body.clientTop
                };
            }
            // 初始化
            scope.init = function(){
                scope.isSaveBtn=false;
                // 人员选择器                
                 scope.stuData = {
                     menu:[{ //进入人员选择器的菜单
                        "id":"User"
                        },{ "id":"Org"},
                        {"id":"Group"},
                        {"id":"Role"}                
                     ],
                     ApiOpen: false,
                     windowModal:1,
                     style: "",                                      
                     ViceuserOrgList: [],
                     ViceuserRoleList: [],
                     ViceuserGroupList: [],
                     searchType: [],  //初始化教师查询
                     //searchTexts: ,  // 初始化教师查询
                     DB: "0",
                     DBdata: [],
                     UserList: [],
                     userOrgList:[],                 
                     userGroupList:[],
                     userRoleList:[]
                 }
                 // 扩展字段
                 if(!scope.comObj.hasOwnProperty('arrage')){
                        scope.comObj.arrage = {};
                 }else if(typeof(scope.comObj.arrage)=="string"){ // 后台返回
                       scope.comObj.arrage = JSON.parse(scope.comObj.arrage);
                 }
                 if( scope.comObj.arrage.userList==undefined){
                    scope.comObj.arrage.userList=[];
                }
                if( scope.comObj.arrage.userOrganization==undefined){
                    scope.comObj.arrage.userOrganization=[];
                }
                if( scope.comObj.arrage.userRole==undefined){
                    scope.comObj.arrage.userRole=[];
                }
                if( scope.comObj.arrage.userGroup==undefined){
                    scope.comObj.arrage.userGroup=[];
                }

                //  // 向人员选择器中赋值
                //  scope.stuData.UserList = scope.comObj.arrage.userList; // 人员
                //  scope.stuData.userOrgList = scope.comObj.arrage.userOrganization;  // 部门
                //  scope.stuData.userRoleList = scope.comObj.arrage.userRole; // 角色
                //  scope.stuData.userGroupList = scope.comObj.arrage.userGroup;   // 组织机构          
                 

            }

            
            scope.init();
           
           // 选择用户群
            scope.selTaskStu = function(event){
                scope.stuData.style=scope.mousePosition(event);
                scope.stuData.ApiOpen = true;     
            }
                  
             // 删除用户群
             scope.selStu = function(item,type){
                if(item.sel==undefined){
                    item.sel=true;
                }else{
                    item.sel=!item.sel;
                }  
             }
            /*****************************人员选择器开始**************************************/

             // 删除用户群
             scope.delStuPub = function(item,type){
                switch (type) {
                    case "1":   // 用户
                         angular.forEach(scope.stuData.UserList,function(data,index,array){
                            if(data.user_ID == item.user_ID){
                                scope.stuData.UserList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                         });                     
                    break;  
                    case "2":    // 部门                    
                         angular.forEach(scope.stuData.userOrgList,function(data,index,array){
                            if(data.organization_ID == item.organization_ID){
                                scope.stuData.userOrgList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                        });
                    break;
                    case "3":   // 角色                     
                      angular.forEach(scope.stuData.userRoleList,function(data,index,array){
                            if(data.roles_ID == item.roles_ID){
                                scope.stuData.userRoleList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                        });
                    break;
                    case "4":   // 用户组                    
                        angular.forEach(scope.stuData.userGroupList,function(data,index,array){
                            if(data.userGroup_ID == item.userGroup_ID){
                                scope.stuData.userGroupList.splice(index,1); //删除当前的
                                return;                           
                            }                                 
                        });
                    break;
                }
             }
             //$watch 接收3个参数，前两个是必选的，第一个为监听的对象或者属性,也可以监听一个函数，第二个为被监听对象或者属性变化时的回调，
             //第三个为true，表示深度监听
            //当为true的时候监听的第一个为对象时，对象的某个属性发生改变也会监听到并触发回调，没写默认false对象某个属性发生改变不会触发回调
            //回调参数接收3个参数，第一个为新值，第二个为旧值,前面监听的是什么回调里的就是前面监听的对象或者属性或者函数
            //监听 部门 角色 用户组 人员 stuData.ApiOpen
            // 处理任务名称
            // scope.isOpenStu = false; // 隔绝第一次
            //  scope.$watch('stuData.ApiOpen', function (newValue, oldValue, scope) {
            //         // 向人员选择器中赋值 全选会出现选择失败的问题 所以要监听一下
            //         scope.comObj.arrage.userList  = scope.stuData.UserList; // 人员
            //         scope.comObj.arrage.userOrganization = scope.stuData.userOrgList;  // 部门
            //         scope.comObj.arrage.userRole = scope.stuData.userRoleList; // 角色
            //         scope.comObj.arrage.userGroup = scope.stuData.userGroupList;   // 组织机构         

            //         scope.isOpenStu = true;
            //  },true);            
            /*****************************人员选择器结束**************************************/            
      
          
            // 保存
            scope.btnSave = function(){
                var param={
                    uid:sysUserInfo.getuserinfo().user_ID,
                    aid:scope.comObj.id,
                    orgs:"",
                    groups:"",
                    roles:"",
                    users:""
                }
                if(!scope.comObj.is_public){
                    if( scope.comObj.arrage.userList.length>0){
                        for (let index = 0; index < scope.comObj.arrage.userList.length; index++) {
                            if(scope.comObj.arrage.userList[index].sel!=undefined&&scope.comObj.arrage.userList[index].sel){
                                param.users+=(param.users.length>0?',':'')+scope.comObj.arrage.userList[index].user_ID;
                            }
                        }
                    }

                    if( scope.stuData.UserList.length>0){
                        for (let index = 0; index < scope.stuData.UserList.length; index++) {
                            param.users+=(param.users.length>0?',':'')+scope.stuData.UserList[index].user_ID;
                        }
                    }

                    if( scope.comObj.arrage.userOrganization.length>0){
                        for (let index = 0; index < scope.comObj.arrage.userOrganization.length; index++) {
                            if(scope.comObj.arrage.userOrganization[index].sel!=undefined&&scope.comObj.arrage.userOrganization[index].sel){
                                param.orgs+=(param.orgs.length>0?',':'')+scope.comObj.arrage.userOrganization[index].organization_ID;
                            }
                        }
                    }
                    if( scope.comObj.arrage.userRole.length>0){
                        for (let index = 0; index < scope.comObj.arrage.userRole.length; index++) {
                            if(scope.comObj.arrage.userRole[index].sel!=undefined&&scope.comObj.arrage.userRole[index].sel){
                                param.roles+=(param.roles.length>0?',':'')+scope.comObj.arrage.userRole[index].roles_ID;
                            }
                        }
                    }
                    if( scope.comObj.arrage.userGroup.length>0){
                        for (let index = 0; index < scope.comObj.arrage.userGroup.length; index++) {
                            if(scope.comObj.arrage.userGroup[index].sel!=undefined&&scope.comObj.arrage.userGroup[index].sel){
                                param.groups+=(param.groups.length>0?',':'')+scope.comObj.arrage.userGroup[index].userGroup_ID;
                            }
                        }
                    }
                }else{
                    if( scope.stuData.UserList.length>0){
                        for (let index = 0; index < scope.stuData.UserList.length; index++) {
                            param.users+=(param.users.length>0?',':'')+scope.stuData.UserList[index].user_ID;
                        }
                    }
                    if( scope.stuData.userOrgList.length>0){
                        for (let index = 0; index < scope.stuData.userOrgList.length; index++) {
                            param.orgs+=(param.orgs.length>0?',':'')+scope.stuData.userOrgList[index].organization_ID;
                        }
                    }
                    if( scope.stuData.userRoleList.length>0){
                        for (let index = 0; index < scope.stuData.userRoleList.length; index++) {
                            param.roles+=(param.roles.length>0?',':'')+scope.stuData.userRoleList[index].roles_ID;
                        }
                    }
                    if( scope.stuData.userGroupList.length>0){
                        for (let index = 0; index < scope.stuData.userGroupList.length; index++) {
                            param.groups+=(param.groups.length>0?',':'')+scope.stuData.userGroupList[index].userGroup_ID;
                        }
                    }
                }

                if(param.users.length<=0&&param.orgs.length<=0&&param.roles.length<=0&&param.groups.length<=0){
                    Msg.warning("请选择要完成的用户！");
                    return;
                }
                scope.isSaveBtn=true;
                RTS.modifyArrayStudy(param).then(function (respose) {
                    scope.isSaveBtn=false;
                    if (respose.errorcode == "0") {
                        Msg.success("操作成功！");
                        scope.onClose();
                    } else if (respose.errorcode == "25") {
                        Msg.warning("任务不存在");
                    } else {
                        Msg.error("操作失败！");
                    }
                }, function errorCallback() {
                    scope.isSaveBtn=false;
                    console.log("连接服务器失败！");
                });

            }

       
         },
         controller: function ($scope, $document) {
             //点击非指定区域，关闭显示层
             $document.bind("click", function (evt) {
                 var evt = window.event ? window.event : evt,
                target = evt.srcElement || evt.target;
                 if ($document[0].getElementById("erjiyunying_fenpeiqiye_middle") != null) {
                     var bt = $document[0].getElementById("erjiyunying_fenpeiqiye_middle").contains(target);
                     if (!bt) {
                         $scope.$apply(function () {
                             $scope.isOrgFlag = false; // 企业搜索列表框
                         });
                     }
                 }
             });
         }
         
     }
 } ]);
